<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="身份证号" prop="staffLicense">-->
<!--        <el-input-->
<!--          v-model="queryParams.staffLicense"-->
<!--          placeholder="请输入身份证号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="居住地址" prop="staffAddress">-->
<!--        <el-input-->
<!--          v-model="queryParams.staffAddress"-->
<!--          placeholder="请输入居住地址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="公司名称" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="手机号码" prop="staffPhone">-->
<!--        <el-input-->
<!--          v-model="queryParams.staffPhone"-->
<!--          placeholder="请输入手机号码"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="驾驶证号" prop="staffCertificate">-->
<!--        <el-input-->
<!--          v-model="queryParams.staffCertificate"-->
<!--          placeholder="请输入驾驶证号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['driver:staff:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['driver:staff:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['driver:staff:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['driver:staff:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="驾驶员编号" align="center" prop="id" width="70px" />
      <el-table-column label="企业名称" align="center" prop="compName" />
      <el-table-column label="姓名" align="center" prop="staffName" />
      <el-table-column label="身份证号" align="center" prop="staffLicense" />
      <el-table-column label="居住地址" align="center" prop="staffAddress" />
      <el-table-column label="手机号码" align="center" prop="staffPhone" />
      <el-table-column label="驾驶证号" align="center" prop="staffCertificate" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['driver:staff:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['driver:staff:remove']"
          >删除</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['driver:staff:edit']"
          >详情</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改驾驶员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="staffLicense">
          <el-input v-model="form.staffLicense" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="居住地址" prop="staffAddress">
          <el-input v-model="form.staffAddress" placeholder="请输入居住地址" />
        </el-form-item>
        <el-form-item label="手机号码" prop="staffPhone">
          <el-input v-model="form.staffPhone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="驾驶证号" prop="staffCertificate">
          <el-input v-model="form.staffCertificate" placeholder="请输入驾驶证号" />
        </el-form-item>

        <!-- 新增公司选择下拉框 -->
        <el-form-item label="公司名称" prop="compId">
          <el-select v-model="form.compId" placeholder="请选择公司" clearable>
            <el-option
              v-for="company in companyList"
              :key="company.id"
              :label="company.compName"
              :value="company.id"
            />
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


       <!--详情对话框-->
    <!-- <el-dialog :title="title" :visible.sync="openDetail" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="staffName">
          <el-input
            v-model="form.staffName"
            placeholder="请输入姓名"
            :disabled="isViewMode"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="staffLicense">
          <el-input
            v-model="form.staffLicense"
            placeholder="请输入身份证号"
            :disabled="isViewMode"
          />
        </el-form-item>
        <el-form-item label="居住地址" prop="staffAddress">
          <el-input
            v-model="form.staffAddress"
            placeholder="请输入居住地址"
            :disabled="isViewMode"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="staffPhone">
          <el-input
            v-model="form.staffPhone"
            placeholder="请输入手机号码"
            :disabled="isViewMode"
          />
        </el-form-item>
        <el-form-item label="驾驶证号" prop="staffCertificate">
          <el-input
            v-model="form.staffCertificate"
            placeholder="请输入驾驶证号"
            :disabled="isViewMode"
          />
        </el-form-item>
        <el-form-item label="公司" prop="compName">
          <el-input
            v-model="form.compName"
            placeholder="请输入公司名称"
            :disabled="isViewMode"
          />
        </el-form-item>

        <el-form-item label="创建时间" prop="createTime">
          <el-input
            v-model="form.createTime"
            placeholder="创建时间"
            disabled
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-show="!isViewMode"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->


    <el-dialog title="驾驶员信息详细" :visible.sync="openDetail" width="800px" append-to-body>
  <el-form ref="form" :model="form" label-width="100px" size="mini">
    <el-row>
      <!-- 左侧列：仅保留有内容的表单项 -->
      <el-col :span="12">
        <el-form-item label="姓名：">{{ form.staffName }}</el-form-item>
        <el-form-item label="身份证号：">{{ form.staffLicense }}</el-form-item>
        <el-form-item label="驾驶证号：">{{ form.staffCertificate }}</el-form-item>
      </el-col>
      <!-- 右侧列：按需求排列 -->
      <el-col :span="12">
        <el-form-item label="居住地址：">{{ form.staffAddress }}</el-form-item>
        <el-form-item label="手机号：">{{ form.staffPhone }}</el-form-item>
        <el-form-item label="公司：">{{ form.compName }}</el-form-item>
      </el-col>
      <!-- 全宽列：创建时间等 -->
      <el-col :span="24">
        <el-form-item label="创建时间：">{{ form.createTime }}</el-form-item>
      </el-col>
    </el-row>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="openDetail = false">关 闭</el-button>
  </div>
</el-dialog>


  </div>
</template>

<script>
import {listStaff, getStaff, delStaff, addStaff, updateStaff, listCompany} from "@/api/driver/staff"

export default {
  name: "Staff",
  data() {
    // 身份证号校验规则
    const validateIdCard = (rule, value, callback) => {
      // 18位身份证号正则表达式
      const idCardReg = /(^\d{18}$)|(^\d{17}(\d|X|x)$)/
      if (!value) {
        return callback(new Error('身份证号不能为空'))
      } else if (!idCardReg.test(value)) {
        return callback(new Error('请输入有效的18位身份证号'))
      } else {
        callback()
      }
    }

    // 手机号校验规则
    const validatePhone = (rule, value, callback) => {
      // 手机号正则表达式，以1开头的11位数字
      const phoneReg = /^1[3-9]\d{9}$/
      if (!value) {
        return callback(new Error('手机号码不能为空'))
      } else if (!phoneReg.test(value)) {
        return callback(new Error('请输入有效的11位手机号码'))
      } else {
        callback()
      }
    }

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾驶员信息表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openDetail: false, // 新增：控制“详情”弹窗
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        staffName: null,
        staffLicense: null,
        staffAddress: null,
        staffPhone: null,
        staffCertificate: null,
        compName: null
      },

      isViewMode: false, // 新增：标记是否为查看模式

      // 公司列表
      companyList: [],

      // 表单参数
      form: {
        id: null,
        staffName: null,
        staffLicense: null,
        staffAddress: null,
        staffPhone: null,
        staffCertificate: null,
        createTime: null,
        compId: null, // 公司ID
        compName: null // 公司名称，用于显示
      },
      // 表单校验
      rules: {
        staffName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        staffLicense: [
          { required: true, validator: validateIdCard, trigger: "blur" }
        ],
        staffAddress: [
          { required: true, message: "居住地址不能为空", trigger: "blur" }
        ],
        staffPhone: [
          { required: true, validator: validatePhone, trigger: "blur" }
        ],
        staffCertificate: [
          { required: true, message: "驾驶证号不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        compId: [
          {  required: true, message: "公司不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getCompanyList() // 加载公司列表
  },
  methods: {
    /** 查询驾驶员信息列表 */
    getList() {
      this.loading = true
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 获取公司列表 */
    getCompanyList() {
      listCompany().then(response => {
        this.companyList = response.rows
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staffName: null,
        staffLicense: null,
        staffAddress: null,
        staffPhone: null,
        staffCertificate: null,
        createTime: null,
        compId: null,
        compName: null
      }
      this.resetForm("form")
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加驾驶员信息"
    },

    // 查看详情
    handleView(row) {
      this.reset()
      const id = row.id
      getStaff(id).then(response => {
        this.form = response.data
        this.isViewMode = true
        this.openDetail = true
        this.title = "驾驶员信息详情"
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getStaff(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改驾驶员信息"
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 找到选中的公司名称并赋值
          const selectedCompany = this.companyList.find(company => company.id === this.form.compId)
          if (selectedCompany) {
            this.form.compName = selectedCompany.compName
          }

          if (this.form.id != null) {
            updateStaff(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除驾驶员信息编号为"' + ids + '"的数据项？').then(function() {
        return delStaff(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('driver/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
